package com.tool_stdy.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @auther: cc.ly
 * @date: 2019/1/30/030
 */

public class AtomicIntegerTest {
    private static final AtomicInteger atomicInteger = new AtomicInteger(0);
    private static final ExecutorService pool = Executors.newFixedThreadPool(10);
    private static final Integer LIMIT = 10;

    public void testRun() throws Exception {
        if (atomicInteger.get() >= LIMIT) {
            throw new IllegalStateException("range limit");
        }
        TimeUnit.MILLISECONDS.sleep(200L);
        if (atomicInteger.get() >= LIMIT) {
            throw new IllegalStateException("range limit");
        }
        int b = atomicInteger.getAndIncrement();
        System.out.println(Thread.currentThread() + "==" + b);
    }

    public static void main(String[] args) {
        AtomicIntegerTest test = new AtomicIntegerTest();
        for (int i = 0; i < 20; i++) {
            pool.submit(() -> {
                try {
                    test.testRun();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
    }

}
